package com.levi.area;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Service
public class TestProvince {
    @Autowired
    private ResourceLoader resourceLoader;

    public void initData(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder jsonStr = null;
        try {
            Resource resource = resourceLoader.getResource("classpath:province.json");
            is = resource.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            jsonStr = new StringBuilder();
            String data;
            while((data = br.readLine()) != null) {
                jsonStr.append(data);
            }
            br.close();
            isr.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String jsonStrBody =jsonStr.toString();
    }

    public void match(){
        InputStream is = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        StringBuilder jsonStr = null;
        try {
            Resource resource = resourceLoader.getResource("classpath:province.json");
            is = resource.getInputStream();
            isr = new InputStreamReader(is);
            br = new BufferedReader(isr);
            jsonStr = new StringBuilder();
            String data;
            while((data = br.readLine()) != null) {
                jsonStr.append(data);
            }
            br.close();
            isr.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }



//        File file = new File("/server/config/"+fileName);
//把读入的文件转换为字符，以便JSON去处理
//String jsonStrBody = FileUtils.readFileToString(file, "UTF-8");
        String jsonStrBody =jsonStr.toString();
//        System.out.println(jsonStrBody);
        List<Province> provinceList = JSON.parseArray(jsonStrBody, Province.class);
//        System.out.println(provinceList);
        List<AreaVO> areaList = new ArrayList<>();
        areaList.add(new AreaVO(1, "河南省", "郑州市", "金水区"));
        areaList.add(new AreaVO(2, "河南省", "郑州市", "新密市"));
        areaList.add(new AreaVO(3, "河南省", "郑州市", "中原区"));

        for (AreaVO areaVO : areaList) {
            flag:for (Province province : provinceList) {
                if (areaVO.getProvince().equals(province.getLabel())){
                    areaVO.setProvince(province.getValue());
                    for (Province city : province.getChildren()) {
                        if (areaVO.getCity().equals(city.getLabel())){
                            areaVO.setCity(city.getValue());
                            for (Province area : city.getChildren()) {
                                if (areaVO.getArea().equals(area.getLabel())){
                                    areaVO.setArea(area.getValue());
                                    break flag;
                                }
                            }
                        }
                    }
                }
            }
        }
        for (AreaVO areaVO : areaList) {
            System.out.println(areaVO);
        }
    }


    public String getJsonStr(){
        return ProvinceCach.getProvinceJsonstr();
    }
}
